#!/bin/bash

# MailServer 编译脚本

echo "开始编译 MailServer..."

# 检查 protoc 是否安装
if ! command -v protoc &> /dev/null; then
    echo "错误: 未找到 protoc 命令"
    echo "请先安装 Protocol Buffers:"
    echo "  Ubuntu/Debian: sudo apt-get install protobuf-compiler libprotobuf-dev"
    echo "  CentOS/RHEL: sudo yum install protobuf-compiler protobuf-devel"
    echo "  macOS: brew install protobuf"
    exit 1
fi

# 生成 protobuf 文件
echo "生成 Protocol Buffers 文件..."
cd proto
if [ -f "protoc.sh" ]; then
    chmod +x protoc.sh
    ./protoc.sh
else
    echo "错误: 未找到 protoc.sh"
    exit 1
fi
cd ..

# 编译
echo "编译 MailServer..."
g++ mail_server.cpp protorpc.c generated/*.cc \
    -I include/hv \
    -I include/hiredis \
    -lhv \
    -lprotobuf \
    -lhiredis \
    -lpthread \
    -o mail

if [ $? -eq 0 ]; then
    echo "编译成功！"
    echo "可执行文件: ./mail"
    echo ""
    echo "运行方法:"
    echo "  ./mail <端口号>"
    echo "  例如: ./mail 1234"
else
    echo "编译失败！"
    echo "请检查依赖库是否正确安装"
    exit 1
fi

